﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace LibFriesland_Campina_CRM
{
    class DalAttendanceTracking
    {
        //Webservice
        //Them theo doi cham cong cua mot NA
        public static int addAttendanceTracking(BizAttendanceTracking pAttendanceTracking)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                //Kiem tra thong tin cham cong co ton tai hay khong
                if (isExistAttendanceTracking(pAttendanceTracking.by_na, pAttendanceTracking.by_shift, pAttendanceTracking.by_time_point, Convert.ToDateTime(pAttendanceTracking.datetime_phone).Date) == 0)
                {
                    AttendanceTracking attendanceTracking = new AttendanceTracking();
                    attendanceTracking.DateTimePhone = Convert.ToDateTime(pAttendanceTracking.datetime_phone);
                    attendanceTracking.DateTimeSystem = Convert.ToDateTime(pAttendanceTracking.datetime_system);
                    attendanceTracking.DateTimeGpS = pAttendanceTracking.datetime_gps;
                    attendanceTracking.Latitude = (float)pAttendanceTracking.latitude;
                    attendanceTracking.Longitude = (float)pAttendanceTracking.longitude;
                    //attendanceTracking.DateTimeMonitoring = DateTime.Now;
                    //attendanceTracking.ActionMonitoring = "";
                    //attendanceTracking.CommentMonitoring = "";
                    attendanceTracking.ByNA = pAttendanceTracking.by_na;
                    attendanceTracking.ByShift = pAttendanceTracking.by_shift;
                    attendanceTracking.ByTimePoint = pAttendanceTracking.by_time_point;

                    attendanceTracking.BySup = BizNA.getSupIDbyNA(pAttendanceTracking.by_na);
                    //attendanceTracking.ByShop = BizNA.getShopIDbyNA(pAttendanceTracking.by_na);
                    attendanceTracking.ByShop = pAttendanceTracking.by_shop;

                    db.AttendanceTracking.InsertOnSubmit(attendanceTracking);
                    db.SubmitChanges();
                    //return attendanceTracking.AttendanceTrackingNumID;
                    return 1;
                }

                return 0;
            }
            catch(Exception ex)
            {
                return -1;
            }
        }

        //Webservice
        //Lay thong tin theo doi cham cong cua NA theo ngay: Mobile
        public static List<BizAttendanceTracking> getAttendanceTrackingbyNA(string pBy_na, DateTime pDatetime_system, string pShop_charid, string pShift_charid)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var attendanceTrackings = from adt in db.AttendanceTracking
                                          where adt.ByNA == pBy_na.Trim() && adt.ByShop == pShop_charid && adt.ByShift == pShift_charid
                                          select new BizAttendanceTracking(
                                                    adt.AttendanceTrackingNumID,
                                                    Thuvien.myConvertDatetimeToString(adt.DateTimePhone),
                                                    Convert.ToDouble(adt.Latitude),
                                                    Convert.ToDouble(adt.Longitude),
                                                    Thuvien.myConvertDatetimeToString(adt.DateTimeMonitoring),
                                                    Thuvien.myConvertNullString(adt.ActionMonitoring),
                                                    Thuvien.myConvertNullString(adt.CommentMonitoring.ToString()),
                                                    adt.ByNA,
                                                    adt.ByShift,
                                                    adt.ByTimePoint,
                                                    getLatlngStatus(adt.ByShop, Convert.ToDouble(adt.Latitude), Convert.ToDouble(adt.Longitude))
                                                );

                List<BizAttendanceTracking> atdList = attendanceTrackings.ToList();
                atdList = atdList.FindAll(
                                        delegate(BizAttendanceTracking atd)
                                        {
                                            if (Convert.ToDateTime(atd.datetime_system).Date == pDatetime_system.Date)
                                                return true;
                                            else
                                                return false;
                                        });
                return atdList;
            }
            catch(Exception ex)
            {
                return null;
            }
        }

        //Lay thong tin theo doi cham cong cua NA theo ngay: Web
        public static List<BizAttendanceTracking> getFullAttendanceTrackingbyNA(string pBy_na, DateTime pDatetime_system, string pShop_charid, string pShift_charid)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var attendanceTrackings = from adt in db.AttendanceTracking
                                          where adt.ByNA == pBy_na.Trim() && adt.ByShop == pShop_charid && adt.ByShift == pShift_charid
                                          select new BizAttendanceTracking(
                                                    adt.AttendanceTrackingNumID,
                                                    Thuvien.myConvertDatetimeToString(adt.DateTimePhone),
                                                    Thuvien.myConvertDatetimeToString(adt.DateTimeSystem),
                                                    Thuvien.myConvertDatetimeToString(adt.DateTimeGpS),
                                                    Convert.ToDouble(adt.Latitude),
                                                    Convert.ToDouble(adt.Longitude),
                                                    Thuvien.myConvertDatetimeToString(adt.DateTimeMonitoring),
                                                    Thuvien.myConvertNullString(adt.ActionMonitoring),
                                                    Thuvien.myConvertNullString(adt.CommentMonitoring.ToString()),
                                                    adt.ByNA,
                                                    adt.ByShift,
                                                    adt.ByTimePoint, 
                                                    adt.ByShop
                                                );

                List<BizAttendanceTracking> atdList = attendanceTrackings.ToList();
                atdList = atdList.FindAll(
                                        delegate(BizAttendanceTracking atd)
                                        {
                                            if (Convert.ToDateTime(atd.datetime_phone).Date == pDatetime_system.Date)
                                                return true;
                                            else
                                                return false;
                                        });
                return atdList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        //Lay thong tin theo doi cham cong cua NA theo ngay
        public static List<BizAttendanceTracking> getAttendanceTrackingbyNA(string pBy_na, DateTime pDatetime_system)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var attendanceTrackings = from adt in db.AttendanceTracking
                                          where adt.ByNA == pBy_na.Trim()
                                          select new BizAttendanceTracking(
                                                    adt.AttendanceTrackingNumID,
                                                    Thuvien.myConvertDatetimeToString(adt.DateTimePhone),
                                                    Convert.ToDouble(adt.Latitude),
                                                    Convert.ToDouble(adt.Longitude),
                                                    Thuvien.myConvertDatetimeToString(adt.DateTimeMonitoring),
                                                    Thuvien.myConvertNullString(adt.ActionMonitoring),
                                                    Thuvien.myConvertNullString(adt.CommentMonitoring.ToString()),
                                                    adt.ByNA,
                                                    adt.ByShift,
                                                    adt.ByTimePoint,
                                                    getLatlngStatus(adt.ByShop, Convert.ToDouble(adt.Latitude), Convert.ToDouble(adt.Longitude))
                                                );

                List<BizAttendanceTracking> atdList = attendanceTrackings.ToList();
                atdList = atdList.FindAll(
                                        delegate(BizAttendanceTracking atd)
                                        {
                                            if (Convert.ToDateTime(atd.datetime_system).Date == pDatetime_system.Date)
                                                return true;
                                            else
                                                return false;
                                        });
                return atdList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        //Webservice
        //Cap nhat thong tin theo doi cham cong cua Supervisor
        public static int updateAttendanceTracking(int pAttendance_tracking_numid, DateTime pDatetime_monitoring, string pAction_monitoring, string pComment_monitoring)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var attendanceTracking = db.AttendanceTracking.Single(p => p.AttendanceTrackingNumID == pAttendance_tracking_numid);
                attendanceTracking.DateTimeMonitoring = pDatetime_monitoring;
                attendanceTracking.ActionMonitoring = pAction_monitoring;
                attendanceTracking.CommentMonitoring = pComment_monitoring;
                db.SubmitChanges();
                
                //return attendanceTracking.AttendanceTrackingNumID;
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        
        //Kiem tra ton tai thong tin theo doi cham cong cua NA: by_shift - by_time_point - datetime_system
        public static int isExistAttendanceTracking(string pBy_na, string pBy_shift, string pBy_time_point, DateTime pDatetime_system)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                List<BizAttendanceTracking> adtList = getAttendanceTrackingbyNA(pBy_na, pDatetime_system.Date);
                int index = adtList.FindIndex(p => p.by_shift == pBy_shift && p.by_time_point == pBy_time_point);
                if (index != -1)
                    return 1;
                return 0;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        //Kiem tra toa do cham cong co phu hop voi toa do shop
        public static bool getLatlngStatus(string pBy_shop, double attendanceTrackingLat, double attendanceTrackingLng)
        { 
            //Lay Latlng cua Shop ma NA lam viec
            //string shop_charid = BizNA.getShopIDbyNA(pNa_charid);
            BizShop shop = BizShop.getShopbyID(pBy_shop);
            double distance = Thuvien.distanceGPS(attendanceTrackingLat, attendanceTrackingLng, shop.latitude, shop.longitude);
            if (distance <= 50)
                return true;
            return false;
        }
    }
}
